package com.me.flow.dto;

import lombok.ToString;

import java.io.Serializable;

@ToString
public class ResultBase<T> implements Serializable {

	private static final long serialVersionUID = -7599731658144653440L;

	/**
	 * 构建成功返回结果
	 */
	public static <T> ResultBase<T> success() {
		return new ResultBase<T>(true, FlowCodeEnum.SUCCESS.getCode(), FlowCodeEnum.SUCCESS.getDesc(), null);
	}

	/**
	 * 构建成功返回结果
	 */
	public static <T> ResultBase<T> success(T t) {
		return new ResultBase<T>(true, FlowCodeEnum.SUCCESS.getCode(), FlowCodeEnum.SUCCESS.getDesc(), t);
	}

	/**
	 * @param type
	 * @param t
	 * @return
	 */
	public static <T> ResultBase<T> success(FlowCodeEnum type, T t) {
		return new ResultBase<T>(true, type.getCode(), type.getDesc(), t);
	}

	/**
	 * @param resultCode
	 * @param resultMsg
	 * @param t
	 * @return
	 */
	public static <T> ResultBase<T> success(String resultCode, String resultMsg, T t) {
		return new ResultBase<T>(true, resultCode, resultMsg, t);
	}

	/**
	 * 构建业务失败返回结果
	 */
	public static <T> ResultBase<T> fail(String resultCode, String resultMsg, T t) {
		return new ResultBase<T>(false, resultCode, resultMsg, t);
	}

	/**
	 * 构建业务失败返回结果
	 */
	public static <T> ResultBase<T> fail(FlowCodeEnum type, T t) {
		return new ResultBase<T>(false, type.getCode(), type.getDesc(), t);
	}

	/**
	 * 构建业务失败返回结果
	 */
	public static <T> ResultBase<T> fail(String resultCode, String resultMsg) {
		return fail(resultCode, resultMsg, null);
	}

	/**
	 * @param e
	 * @return
	 */
	public static <T> ResultBase<T> pasteResponseCodeEnum(FlowCodeEnum e) {
		return new ResultBase<T>(e.getCode(), e.getDesc());
	}

	/**
	 * @param e
	 * @param t
	 * @return
	 */
	public static <T> ResultBase<T> pasteResponseCodeEnum(FlowCodeEnum e, T t) {
		return new ResultBase<T>(e.getCode(), e.getDesc(), t);
	}

	/**
	 * 构建业务失败返回结果
	 */
	public static <T> ResultBase<T> fail(FlowCodeEnum type) {
		return fail(type.getCode(), type.getDesc(), null);
	}

	/**
	 * @return
	 */
	public boolean isSuccess() {
		return success;

	}

	/**
	 * @param success
	 * @param resultCode
	 * @param resultMsg
	 * @param value
	 */
	public ResultBase(boolean success, String resultCode, String resultMsg, T value) {
		super();
		this.success = success;
		this.resultCode = resultCode;
		this.resultMsg = resultMsg;
		this.value = value;
	}

	public ResultBase(String resultCode, String resultMsg, T value) {
		super();
		this.resultCode = resultCode;
		this.resultMsg = resultMsg;
		this.value = value;
	}

	public ResultBase(String resultCode, String resultMsg) {
		super();
		this.resultCode = resultCode;
		this.resultMsg = resultMsg;
	}

	/**
	 * @description 业务是否处理成功标志
	 */
	private boolean success = false;

	/**
	 * 错误代码（业务）
	 */
	private String resultCode;

	/**
	 * 错误代码描述（业务）
	 */
	private String resultMsg;

	/**
	 * 业务数据
	 */
	private T value;

	public ResultBase() {
		super();
	}

	/**
	 * @return the resultCode
	 */
	public String getResultCode() {
		return resultCode;
	}

	/**
	 * @param resultCode
	 *            the resultCode to set
	 */
	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	/**
	 * @return the resultMsg
	 */
	public String getResultMsg() {
		return resultMsg;
	}

	/**
	 * @param resultMsg
	 *            the resultMsg to set
	 */
	public void setResultMsg(String resultMsg) {
		this.resultMsg = resultMsg;
	}

	/**
	 * @return the value
	 */
	public T getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(T value) {
		this.value = value;
	}

	/**
	 * @param success
	 *            the success to set
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}

}